package com.bupt.kgplatform.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.bupt.kgplatform.entity.pojo.MinioPojo;

import com.bupt.kgplatform.service.MinioService;
import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * ClassName:MinioServiceImpl
 * Package:com.example.blackjavaweb.service.impl
 * Description:
 *
 * @Author QIQI_1999
 * @Creat 2023/10/15 11:08
 * @Version 1.0
 */
@Service
public class MinioServiceImpl implements MinioService {

    @Override
    public Object getFileName(MinioPojo minioPojo) throws InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, IOException, InvalidExpiresRangeException, InvalidKeyException, XmlParserException, InvalidResponseException, InternalException, InvalidPortException, InvalidEndpointException, ServerException {
        MinioClient minioClients = new MinioClient("http://10.112.109.177:9000/", "minio", "minio123");
        String fileUrl;
        if("document".equals(minioPojo.getType())){
            fileUrl = minioClients.presignedGetObject("hetaishi","ctbto/document/"+minioPojo.getFileName());
        } else if ("translate".equals(minioPojo.getType())) {
            fileUrl = minioClients.presignedGetObject("hetaishi","ctbto/translate/"+minioPojo.getFileName());
        }else {
            fileUrl = null;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fileUrl",fileUrl);
        return jsonObject;
    }
}
